const {app,BrowserWindow,ipcMain,Menu}=require('electron')
const cfg=require('./appjs/app_cfg.js')
const ipcs=require('./appjs/ipcs.js')
const path=require("path")
let argv=process.argv

app.disableHardwareAcceleration()

const hasLock=app.requestSingleInstanceLock()

if (!hasLock){
  app.quit()
}

for (let item of ipcs){
  ipcMain[item.type](item.name,item.func)
}

ipcMain.handle('getArgvFile',()=>{
  let filePath=argv[argv.length-1]
  if (filePath.indexOf('mp4')!==-1){
    return filePath
  }else{
    return ''
  }
})

app.whenReady().then(()=>{
  if (process.platform==='win32'){
    app.setAppUserModelId(process.execPath)
  }
  createWindow()
})

app.on('second-instance',(event,cmd)=>{
  event.preventDefault()
  argv=cmd
  createWindow()
})

app.on('window-all-closed',()=>{
  app.quit()
})

async function createWindow(){
  const win=new BrowserWindow({
    show:false,
    width:1280,
    height:756,
    // fullscreen:true,
    // transparent:true,
    frame:false,
    webPreferences:{
      preload:path.resolve(__dirname,'./appjs/preload.js'),
      devTools:false,
      nodeIntegration:false,
      contextIsolation:true
    }
  })
  Menu.setApplicationMenu(null)
  win.webContents.openDevTools()

  await win.loadFile(cfg.entry)
  win.show()

}